package com.avexVertex.controller;

import com.avexVertex.pojo.Emp;
import com.avexVertex.pojo.EmpQueryParam;
import com.avexVertex.pojo.PageResult;
import com.avexVertex.pojo.Result;
import com.avexVertex.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * 版本一: 参数多
     *
     * @return
     */
    //@GetMapping
    public Result page(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @RequestParam(value = "name", defaultValue = "", required = false) String name,
            @RequestParam(value = "gender", defaultValue = "", required = false) Integer gender,
            @RequestParam(value = "begin", defaultValue = "", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @RequestParam(value = "end", defaultValue = "", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end) {
        log.info("分页查询:{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
        //PageResult<Emp> pageResult = empService.page(page, pageSize);
        PageResult<Emp> pageResult = empService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pageResult);
    }

    /**
     * 分页查询
     * 版本二: 参数少、简洁
     *
     * @param empQueryParam
     * @return
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        //PageResult<Emp> pageResult = empService.page(page, pageSize);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增员工
     *
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工:{}", emp);

        empService.save(emp);

        return Result.success();
    }


    /**
     * 删除员工 -- 数组接收数据
     *
     * @return
     */
    //@DeleteMapping
    public Result delete(@RequestParam("ids") Integer[] ids) {
        log.info("删除员工:{}", Arrays.toString(ids));

        return Result.success();
    }


    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Integer> ids) {
        log.info("删除员工:{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID来查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") Integer id) {
        log.info("根据ID查询员工信息:{}", id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工:{}",emp);
        empService.update(emp);
        return Result.success();
    }

}
